package com.hangzhou.xyj.ui.adapter.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by neo.duan on 2016/3/2.
 * 所有recyclerView的Adapter基类
 */
public abstract class AbsBaseAdapter<T extends Object> extends RecyclerView.Adapter {

    public List<T> mDataList;
    public Context mContext;
    public LayoutInflater mLayoutInflater;
    public int mIntentTag;

    public AbsBaseAdapter(Context context, List<T> dataList) {
        this.mContext = context;
        if (dataList == null) {
            dataList = new ArrayList<>();
        }
        this.mDataList = dataList;
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /**
     * 区分业务
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/15 9:40
     */
    public void setIntentTag(int intentTag) {
        this.mIntentTag = intentTag;
    }

    /**
     * 刷新数据
     *
     * @param newDataList
     */
    public void update(List<T> newDataList) {
        if (newDataList == null && newDataList.isEmpty()) {
            newDataList = new ArrayList<>();
        }
        this.mDataList = newDataList;
        notifyDataSetChanged();
    }

    /**
     * 新增数据集合
     *
     * @param newDataList
     */
    public void add(List<T> newDataList) {
        if (newDataList == null && newDataList.isEmpty()) {
            return;
        }
        this.mDataList.addAll(newDataList);
        notifyDataSetChanged();
    }

    /**
     * 增加一条数据
     *
     * @param t
     */
    public void add(T t) {
        if (t == null) {
            return;
        }
        this.mDataList.add(t);
        notifyDataSetChanged();
    }

    /**
     * 向某一位置增加数据
     *
     * @param position
     * @param t
     */
    public void add(int position, T t) {
        if (t == null) {
            return;
        }
        this.mDataList.add(position, t);
        notifyDataSetChanged();
    }

    /**
     * 删除数据
     *
     * @param removeDataList
     */
    public void remove(List<T> removeDataList) {
        if (removeDataList == null || removeDataList.isEmpty()) {
            return;
        }
        if (mDataList.containsAll(removeDataList)) {
            mDataList.removeAll(removeDataList);
        }
        notifyDataSetChanged();
    }

    /**
     * 删除某个对象数据
     *
     * @param t
     */
    public void remove(T t) {
        if (t == null) {
            return;
        }
        if (mDataList.contains(t)) {
            mDataList.remove(t);
        }
        notifyDataSetChanged();
    }

    /**
     * 删除某个位置上的数据
     *
     * @param position
     */
    public void remove(int position) {
        if (position < 0 || mDataList.size() < position) {
            return;
        }
        mDataList.remove(position);
        notifyDataSetChanged();
    }

    /**
     * 清空数据
     */
    public void clear() {
        this.mDataList.clear();
        notifyDataSetChanged();
    }

    /**
     * 创建ViewHolder
     *
     * @param viewGroup
     * @param viewType
     * @return
     */
    public abstract RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType);

    /**
     * 对ViewHolder绑定数据
     *
     * @param viewHolder
     * @param position
     */
    @Override
    public abstract void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position);

    /**
     * 获取整个Adapter的item count  可复写
     *
     * @return
     */
    @Override
    public int getItemCount() {
        if (mDataList == null || mDataList.isEmpty()) {
            return 0;
        }
        return mDataList.size();
    }

    /**
     * 获取ViewType类型，可复写
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }
}
